package com.team5.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.team5.entity.News;
import com.team5.response.Code;
import com.team5.response.Result;
import com.team5.service.NewsService;
import com.team5.websocket.NewsWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

@RequestMapping("/news")
@RestController
@CrossOrigin(origins = "http://localhost:8081")
public class NewsController {
    @Autowired
    private NewsService service;

    @Autowired
    private NewsWebSocketHandler newsWebSocketHandler;

    @RequestMapping("/findById")
    public Result findByID(int id) {
        News news = service.findById(id);
        return new Result(Code.SELECT_OK, news);
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        News[] news = service.findAll();
        return new Result(Code.SELECT_OK, news);
    }

}
